package com.ts.simplemusic;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.ViewHolder> {

    private final List<Music> mMusicList;
    private Context mContext;

    private int currentPosition;

    private onClickLister mClickLister;

    public MusicAdapter(List<Music> musics) {
        mMusicList = musics;
    }

    @NonNull
    @Override
    public MusicAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_item, parent, false);
        mContext = parent.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MusicAdapter.ViewHolder holder, int position) {
        Music music = mMusicList.get(position);
        holder.musicPosition.setText(String.format(mContext.getString(R.string.music_position), position + 1));
        holder.musicName.setText(music.getTitle());
        if (position == MusicHelper.getInstance().getCurrentPosition()) {
            holder.musicPlayState.setVisibility(View.VISIBLE);
            holder.musicPosition.setVisibility(View.INVISIBLE);
            holder.musicName.setTextColor(mContext.getColor(R.color.music_red));
        } else {
            holder.musicPlayState.setVisibility(View.INVISIBLE);
            holder.musicPosition.setVisibility(View.VISIBLE);
            holder.musicName.setTextColor(mContext.getColor(R.color.black));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notifyItemChanged(MusicHelper.getInstance().getCurrentPosition(), 0);
                MusicHelper.getInstance().setCurrentPosition(holder.getAdapterPosition());
                currentPosition = holder.getAdapterPosition();
                notifyItemChanged(MusicHelper.getInstance().getCurrentPosition(), 0);
                if (mClickLister != null) {
                    mClickLister.replace();
                }
            }
        });
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    @Override
    public int getItemCount() {
        return mMusicList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView musicName;

        TextView musicPosition;

        ImageView musicPlayState;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            musicPosition = itemView.findViewById(R.id.music_position);
            musicName = itemView.findViewById(R.id.music_name);
            musicPlayState = itemView.findViewById(R.id.playing_state_icon);
        }
    }

    public void setClickLister(onClickLister clickLister) {
        mClickLister = clickLister;
    }

    public interface onClickLister {
        void replace();
    }
}
